package com.ms.p2p.template.method;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.p2p.provider.SettingProvider;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by xing_ on 2017/5/23.
 */
@Repository
public class GlobalMethod implements TemplateMethodModelEx {
    private static String prex = "";

    @Reference
    private SettingProvider settingProvider;

    @Override
    public Object exec(List arguments) throws TemplateModelException {

        if (arguments != null && !arguments.isEmpty()  ) {

            String nid = String.valueOf(arguments.get(0));
            String s = settingProvider.findValueByCode(prex+nid);

            if(StringUtils.isBlank(s)){
                return null;
            }
            return new SimpleScalar(s);
        }

        return null;
    }
}
